package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.marketBean.MarketUser;
import com.cskaoyan.bean.vo.zxw.wx.WxFootPrintListEntityVo;
import com.cskaoyan.bean.vo.zxw.wx.WxFootPrintVo;
import com.cskaoyan.service.wx.WxFootPrintService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 历史足迹模块
 *
 * @author 曾添乐啦
 * @since 2023/01/01 21:49
 */
@RestController
@RequestMapping("wx/footprint")
public class WxFootPrintController {

    @Autowired
    WxFootPrintService wxFootPrintService;

    @RequestMapping("list")
    public BaseRespVo selectUserAllFootprint(BasePageInfo basePageInfo) {
        //从session域拿到用户信息
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipal();

        Integer userId = marketUser.getId();

        //WxFootPrintVo wxFootPrintVo = wxFootPrintService.selectUserAllFootprint(basePageInfo, userId);
        CommonData<WxFootPrintListEntityVo> commonData = wxFootPrintService.selectUserAllFootprint(basePageInfo, userId);
        return BaseRespVo.ok(commonData);
    }

    /**
     * Request URL: http://localhost:8083/wx/footprint/delete
     * 需要长按小程序历史足迹，才能选中所选历史足迹进行删除
     *
     * @return
     */
    @RequestMapping("delete")
    public BaseRespVo deleteSelectedFootprint(@RequestBody WxFootPrintListEntityVo wxFootPrintListEntityVo) {
        int id = wxFootPrintListEntityVo.getId();
        int deleteAffected = wxFootPrintService.deleteSelectedFootprint(id);
        if (deleteAffected == 1) {
            return BaseRespVo.ok("success");
        }
        return BaseRespVo.invalidParameter("删除历史足迹失败，系统繁忙请稍后再试");
    }
}
